/*
   CashDrawerControl.cpp
   
   Copyright (C) 2006 LinuxOne, Inc. <Republic of korea>

   This library is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1
   of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Lessor General Public License for more details.

   You should have received a copy of the GNU Lessor General Public
   License along with this library; if not, write to the Free Software
   Foundation, inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

   Hyunjin Kim <mgc521@linuxone.co.kr>

   LinuxOne, Inc., hereby disclaims all copylight
   written by Hyunjin Kim, 20 December 2006
*/

#include <control/CashDrawerControl.h>

namespace booyopos
{

CCashDrawer::CCashDrawer()
{
	deviceControlDescription = "BooyoPOS CashDrawer Device Control";
	deviceControlVersion = 0;

	cds= new services::CCashDrawerService;

	CBaseControl::bs=dynamic_cast<services::CBaseService *>(cds);
}

CCashDrawer::~CCashDrawer()
{
}

/* Capabilities */
bool
CCashDrawer::GetCapStatus()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetCapStatus();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

int
CCashDrawer::GetCapPowerReporting()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetCapPowerReporting();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

bool
CCashDrawer::GetCapStatusMultiDrawerDetect()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetCapStatusMultiDrawerDetect();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

bool
CCashDrawer::GetCapStatisticsReporting()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetCapStatisticsReporting();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

bool
CCashDrawer::GetCapUpdateStatistics()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetCapUpdateStatistics();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

bool
CCashDrawer::GetCapCompareFirmwareVersion()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetCapCompareFirmwareVersion();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

bool
CCashDrawer::GetCapUpdateFirmware()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetCapUpdateFirmware();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

/* Properties */
bool
CCashDrawer::GetDrawerOpened()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetDrawerOpened();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

int
CCashDrawer::GetPowerNotify()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetPowerNotify();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void
CCashDrawer::SetPowerNotify(int powerNotify)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		cds->SetPowerNotify(powerNotify);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

int
CCashDrawer::GetPowerState()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		return cds->GetPowerState();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

/* Methods */
void
CCashDrawer::OpenDrawer()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		cds->OpenDrawer();
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void
CCashDrawer::WaitForDrawerClose(int beepTimeout, int beepFrequency, int beepDuration, int beepDelay)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		cds->WaitForDrawerClose(beepTimeout, beepFrequency, beepDuration, beepDelay);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

/* Statistics */
void
CCashDrawer::ResetStatistics(std::string statisticsBuffer)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		cds->ResetStatistics(statisticsBuffer);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void
CCashDrawer::RetrieveStatistics(std::string statisticsBuffer[])
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		cds->RetrieveStatistics(statisticsBuffer);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void
CCashDrawer::UpdateStatistics(std::string statisticsBuffer)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		cds->UpdateStatistics(statisticsBuffer);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void
CCashDrawer::CompareFirmwareVersion(std::string firmwareFileName, int* result)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		cds->CompareFirmwareVersion(firmwareFileName, result);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

void
CCashDrawer::UpdateFirmware(std::string firmwareFileName)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Control not opened");
		}

		cds->UpdateFirmware(firmwareFileName);
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(std::exception e)
	{
		throw CByException(E_FAILURE, "Unhandled exception from Device Service", e);
	}
}

} // namespace booyopos
